#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# visualisering för klubb melt
# kristoffer josefsson 2005

from vj import *
from texturegl import *

class BridgeCubeRecreator(RecreatingCubeRecreator):

	def newside(self):
		i = randint(0,15)
#		if i in range(0,5):
#			return TTFBillboard("melt","bloktilt.ttf", 30)
		if i in range(5,6):
			return TTFBillboard("visuals: krute.com", "register.ttf",9)
		else:
			return BridgeBillboard("anders.jpg")

class Bridge(Scene):
	def	__init__(self):
		Scene.__init__(self)
		
		self.title = "Melt"
		
		self.rtri =	0.5
		self.triRate = 3.0
		self.raxis = [0,1,0]
		self.fov = 30
		self.dizzy = False

	def regenerate(self):
		
		Scene.regenerate(self)
		
		i = randint(0,100)
			
		if i in range(0,30):
			self.dizzy = True
		else:	
			self.dizzy = False

		if i in range(10,40):
			self.triRate = randint(-5,5)
			if self.triRate == 0:
				self.dizzy = True		
		if i in range(60,100):
			self.fov = randint(20,170)
		if i in range(0,100):
			self.interval = randint(10,100)

		if i in range(30,65):
			self.bill.recreate()

		if i in range(0,30):
			self.raxis = [1,0,0]
		if i in range(31,62):
			self.raxis = [0,1,0]
		if i in range(63,100):
			self.raxis = [0,0,1]

	def	initLighting(self):
		# from peters lightsetup
		glShadeModel(GL_SMOOTH)

		position=[
			[ -.5, 0.0,	0.0, 0.0],
			[ 0.0, -.5,	0.0, 0.0],
			[ 0.0, 0.0,	0.5, 0.0],
			[ 0.0, 0.0,	0.0, 0.0]
		]
		diffuse=[
			[ 0.0,	0.4,  0.6, 1.0],
			[ 0.0,	0.6,  0.4, 1.0],
			[ 0.4,	0.6,  0.0, 1.0],
			[ 0.6,	0.4,  0.0, 1.0]
		]
		specular=[
			[ 0.9,	0.0,  0.4, 1.0],
			[ 0.4,	0.7,  0.0, 1.0],
			[ 0.0,	0.7,  0.4, 1.0],
			[ 0.4,	0.0,  0.9, 1.0]
		]
		ambient=[0.07, 0.07, 0.07, 1.0]

		for	i in range(4):
			glLightfv(GL_LIGHT0+i, GL_POSITION,	position[i])
			glLightfv(GL_LIGHT0+i, GL_DIFFUSE, diffuse[i])
			glLightfv(GL_LIGHT0+i, GL_SPECULAR,	specular[i])
			glLightfv(GL_LIGHT0+i, GL_AMBIENT, ambient)
			glEnable(GL_LIGHT0+i)

		glEnable(GL_LIGHTING)

	def	init(self, width, height):
	
		Scene.init(self, width, height)

	#	glEnable(GL_BLEND)
	#	glBlendFunc(GL_SRC_ALPHA,	GL_ONE_MINUS_SRC_ALPHA)
	#	glBlendFunc(GL_ONE, GL_DST_ALPHA)
	#	glEnable(GL_LINE_SMOOTH)
	#	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
	#	glEnable(GL_POLYGON_SMOOTH)
		glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)

		self.initLighting()

		self.bill = RecreatingCube(BridgeCubeRecreator)

		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()					# reset	the	projection matrix
		gluPerspective(self.fov, float(width)/float(height), 0.1, 100.0)
						# calculate	aspect ratio of	window
		glMatrixMode(GL_MODELVIEW)

	def	display(self):
		Scene.display(self)
		
		glRotatef(self.rtri,self.raxis[0],self.raxis[1],self.raxis[2])   # rotate	pyramid	about its y-axis
		glTranslatef(-0.5,-0.5,0.5)		 # move	left and into screen
		
		if self.dizzy == True:
			glScalef(1+sin(self.t1*2)/8,1+sin(self.t1*4)/8,1+sin(self.t1*8)/8);

		self.bill.draw()
		
		glLoadIdentity()
		glTranslate(0,0,-13)
		
		glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT)
		glDisable(GL_DEPTH_TEST)
		glDisable(GL_LIGHTING)
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND)
		glPopAttrib()


		glutSwapBuffers()


	def	keyboard(self, key,	x, y):
		Scene.keyboard(self, key, x,y)

		if key == '1':
			self.bill.recreate()

		if key == '2':
			self.regenerate()
			
		if key == 'x':
			self.raxis = [1,0,0]
		if key == 'y':
			self.raxis = [0,1,0]
		if key == 'z':
			self.raxis = [0,0,1]
		
		if key == '+':
			self.triRate += 1
		if key == '-':
			self.triRate -= 1	

		if key == 'f':
			self.fov += 1
		if key == 'v':
			self.fov -= 1

		if key == 's':
			if self.dizzy == False:
				self.dizzy = True
			else:
				self.dizzy = False


if __name__=="__main__":
	
#	try:
#        	import psyco
#		psyco.full()
#   	except:
# 	     	print 'Psyco not found, ignoring it'


	s=Bridge()
	launchConsole()
	s.main(sys.argv)
	
